<?php

namespace app\common\model;


class MallGoods extends Base
{

    protected $name = "mall_goods";

    protected $deleteTime = "delete_time";


    public function mallCate()
    {
        return $this->belongsTo('\app\common\model\MallCate', 'cate_id', 'id');
    }

    public function shop()
    {
        return $this->hasOne(MallShop::class, 'id', 'shop_id');
    }

    /**
     * @param int $cate_id
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMallGoodsByCateId(int $cate_id = 0, string $field = '*'): array
    {
        if ($cate_id == 0) {
            return [];
        }
        return $this->field($field)
            ->where('cate_id', $cate_id)
            ->where('status', 1)
            ->select()
            ->toArray();
    }

    /**
     * 查询首页商品信息
     * @param array $cate_ids
     * @param int $limit
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function goodsIndexList(array $cate_ids = [], int $limit = 10, string $field = '*'): array
    {
        return $this->field($field)
            ->where('cate_id', "in", $cate_ids)
            ->order('sort desc,id desc')
            ->limit($limit)
            ->select()
            ->toArray();
    }

    /**
     * @param array $ids
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoodsByIds(array $ids = []): array
    {
        return $this
            ->whereIn('id', $ids)
            ->with('shop')
            ->select()
            ->toArray();
    }

    /**
     * @param array $cate_ids
     * @param int $shop_id
     * @param int $page
     * @param int $limit
     * @param string $keywords
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getGoodsCateIds(array $cate_ids = [], int $shop_id = 0, int $page = 1, int $limit = 10,string $keywords = "",string $openid = "")
    {
        $where = [];
        if ($shop_id != 0) {
            $where[] = [
                ['shop_id', '=', $shop_id]
            ];
        }

        if(!empty($keywords)){
            $where[] = [
                ['title', 'like', "%{$keywords}%"]
            ];
        }else{
            if (!empty($cate_ids)) {
                $where[] = [
                    ['cate_id', 'in', $cate_ids]
                ];
            }
        }
        return $this->field('id,title,thumb_title,price,sku_id,logo,specifications,shop_id')
            ->where($where)
            ->order('sort desc,id desc')
            ->paginate($limit)->each(function ($item,$index){
                //获取选择数量
                $cateCart = (new MallCateCart())->field('id,sum(total) as total')->where('openid',request()->openid)->where('goods_id',$item['id'])->find();
                $item['cateCartTotal'] = $cateCart['total'] ?? 0;
                $item['cateCartId'] = $cateCart['id'] ?? 0;
                return $item;
            })
            ->toArray();
    }

}